/**
 * @description 本地存储
 * @param {key,value} 传入一个键值对
 */
export const LocalSave = (key, value) => {
  return new Promise((resolve, reject) => {
    try {
      localStorage.setItem(key, JSON.stringify(value));
      resolve("存储成功"); // 或者您可以自定义成功信息
    } catch (error) {
      reject(error); // 存储失败时返回错误信息
    }
  });
};

/**
 * @description 本地存储
 * @param key传入一个键
 */
export const LocalGet = (key) => {
  return localStorage.getItem(key);
};

export const LocalClear = (key) => {
  localStorage.removeItem(key);
};

/**
 * @description 本地存储获取值
 * @param {string} key 存储的键名
 * @returns {Promise<any>} 存储的值的 Promise 对象，若未找到对应键的值则返回 null
 */
export const LocalGetPro = (key) => {
  return new Promise((resolve, reject) => {
    try {
      const value = localStorage.getItem(key);
      resolve(value);
    } catch (error) {
      reject(error);
    }
  });
};

/**
 * @description 本地存储清除值
 * @param {string} key 要清除的键名
 * @returns {Promise<void>} 清除操作的 Promise 对象
 */
export const LocalClearPro = (key) => {
  return new Promise((resolve, reject) => {
    try {
      localStorage.removeItem(key);
      resolve();
    } catch (error) {
      reject(error);
    }
  });
};

/**
 * @description 该方法只适用于当前项目
 * @returns {token} token
 */
export const GetToken = () => {
  let tokenTemp;
  let saveData;
  LocalGetPro("UserStore")
    .then((res) => {
      res = res.trim();
      let temp = JSON.parse(res);
      saveData = temp.userInfo;
      let { token } = { ...saveData };
      tokenTemp = token;
      if (tokenTemp == undefined || tokenTemp == "") {
        tokenTemp = "TestToken";
      }
    })
    .catch((err) => {
      console.error("本地没有该数据");
    });
  return tokenTemp;
};
